﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HA.Data;
using System.Collections.ObjectModel;

namespace HA.ModFlow.Controls.UserControls
{
    /// <summary>
    /// ucGridMultiMapData.xaml 的交互逻辑
    /// </summary>
    public partial class winGridMultiMapData : Window
    {
        public winGridMultiMapData(List<BCDateData> valueDatas)
        {
            InitializeComponent();
            foreach (var item in valueDatas)
            {
                _valueDatas.Add((BCDateData)item.Clone());
            }
            if (_valueDatas.Count > 0)
                _valueDatas[0].PreData = null;
            if (_valueDatas.Count > 1)
            {
                _valueDatas[0].AfterData = _valueDatas[1];
            }
            for (int i = 1; i < _valueDatas.Count - 1; i++)
            {
                _valueDatas[i].PreData = _valueDatas[i - 1];
                _valueDatas[i].AfterData = _valueDatas[i + 1];
            }
            if (_valueDatas.Count == 1)
            {
                _valueDatas[0].AfterData = null;
            }
            if (_valueDatas.Count > 1)
            {
                _valueDatas[_valueDatas.Count - 1].PreData = _valueDatas[_valueDatas.Count - 2];
                _valueDatas[_valueDatas.Count - 1].AfterData = null;
            }

            this.dtGrid.ItemsSource = ValueDatas;

        }

        ObservableCollection<BCDateData> _valueDatas = new ObservableCollection<BCDateData>();

        public ObservableCollection<BCDateData> ValueDatas
        {
            get { return _valueDatas; }
            set { _valueDatas = value; }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (_valueDatas.Count == 0)
                this._valueDatas.Add(new BCDateData(1, 1));
            else
            {
                this._valueDatas.Add(new BCDateData(_valueDatas[_valueDatas.Count - 1].EndTime + 1, _valueDatas[_valueDatas.Count - 1].EndTime + 1) {PreData = _valueDatas[_valueDatas.Count-1] });
                this._valueDatas[_valueDatas.Count - 2].AfterData = this._valueDatas[_valueDatas.Count - 1];
            }


            this.dtGrid.Items.Refresh();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            int index = this.dtGrid.SelectedIndex;

            if (index >= 0)
            {
                this._valueDatas.RemoveAt(index);

                if (index == 0 && this._valueDatas.Count >= 1)
                {
                    this._valueDatas[0].PreData = null;
                    this._valueDatas[0].StartTime = 1;
                }
                else if (index < _valueDatas.Count)
                {
                    this._valueDatas[index].PreData = this._valueDatas[index - 1];
                    this._valueDatas[index - 1].AfterData = this._valueDatas[index];
                    this._valueDatas[index].StartTime = this._valueDatas[index - 1].EndTime + 1;
                }
                else
                {
                    if (index > 0)
                        this._valueDatas[index - 1].AfterData = null;
                }

                //if (this._valueDatas.Count <= 1)
                //{
                //    this.btnDelete.IsEnabled = false;

                //}



                this.dtGrid.SelectedIndex = index - 1;

                this.dtGrid.Items.Refresh();



            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;

            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void dtGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            try
            {
                this.dtGrid.Items.Refresh();
            }
            catch { }
        }




    }
}
